package miku.firstgame.ui.render
{
	import feathers.controls.Label;
	import feathers.controls.renderers.DefaultListItemRenderer;

	import miku.firstgame.model.Me;
	import miku.firstgame.model.User;

	public class UserPrepareItemRender extends DefaultListItemRenderer
	{

		public var prepareLabel:Label;
		public var masterLabel:Label;

		public function UserPrepareItemRender()
		{
			super();
		}

		override protected function initialize():void
		{
			super.initialize();
			createChildren();
		}

		protected function get user():User
		{
			return this.data as User;
		}

		protected function createChildren():void
		{
			prepareLabel = new Label();
			prepareLabel.text = "未准备";
			addChild(prepareLabel);
			prepareLabel.validate();

			masterLabel = new Label();
			masterLabel.text = "";
			addChild(masterLabel);
			masterLabel.validate();
		}

		override protected function commitData():void
		{
			super.commitData();
			refreshData();
		}

		override protected function draw():void
		{
			super.draw();
			var sizeInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_SIZE);
			if (sizeInvalid)
			{
				resize();
			}
//			var dataInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_DATA);
//			if (dataInvalid)
//			{
//				refreshData();
//			}
		}

		protected function refreshData():void
		{
			if (user)
			{
				if (user.isReady)
				{
					prepareLabel.text = "已准备";
				}
				else
				{
					prepareLabel.text = "未准备";
				}

				if (Me.Instace.room && Me.Instace.room.roomMaster && user.id == Me.Instace.room.roomMaster.id)
				{
					masterLabel.text = "房主";
				}
				else
				{
					masterLabel.text = "不是房主";
				}

			}
		}

		protected function resize():void
		{
			prepareLabel.x = this.width - prepareLabel.width;
			prepareLabel.y = this.height - prepareLabel.height;

			masterLabel.x = 0;
			masterLabel.y = 0;
		}
	}
}
